package chapter14;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/22 21:20
 */
class Parent {
    public String name;
    @JsonManagedReference
    public Son son;
}

class Son {
    public String name;
    @JsonBackReference
    public Parent parent;
}

public class JacksonTest2 {
    public static void main(String[] args) {
        Parent parent = new Parent();
        parent.name = "laoma";
        Son son = new Son();
        son.name = "xiaoma";
        parent.son = son;
        son.parent = parent;

        ObjectMapper mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        try {
            String xml = mapper.writeValueAsString(parent);
            System.out.println(xml);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}
